<?php

class User_model extends CI_Model {
    
    function login($username, $password) {
        $this->db->where("username", $username);
        $this->db->where("password", $password);
        $query = $this->db->get("user");
        if ($query->num_rows() > 0) {
            $this->session->set_userdata('user',$query->row());
            $id = $query->row()->id;
            $role = $query->row()->role;
            $data['uid'] = $id;
            $data['username'] = $username;
            $data['logintime'] = time();
            $this->db->insert("lfsrlzy_login",$data);
            if($role == 1){
                $this->db->where("userid", $id);
                $query2 = $this->db->get("employee");
                if($query2->num_rows() > 0){
                    $this->session->set_userdata('employee',$query2->row());
                    $this->session->set_userdata('department',$query2->row()->department);
                }else{
                    $this->session->set_userdata('employee',null);
                    $this->session->set_userdata('department',0);
                }
            }else{
                $this->session->set_userdata('employee',null);
                $this->session->set_userdata('department',0);
            }            
            return true;
        }
        return false;
    }
    
    public function getuserbyid($id){
        $query = $this->db->get_where('user', array('id' => $id));
        return $query->row_array();
    }
    
    public function getEmployeeById($id){
        $query = $this->db->get_where('employee', array('id' => $id));
        return $query->row_array();
    }
    
    public function get_user($offset,$num,$order='id desc',$condition){
        $table   = "lfsrlzy_user";
        $str_sql  = "select * from {$table} where 1=1 and username like '%".$condition."%' and username != 'admin'  order by {$order} limit {$offset},{$num}";
        $this->db->like('username', $condition);
        $this->db->from('user');
        return array(
            'total' => $this->db->count_all_results(),
            'res' => $this->db->query($str_sql)->result_array(),
        ); 
    }

    public function get_login_log($offset,$num,$order='id desc',$condition){
        $table   = "lfsrlzy_login";
        $str_sql  = "select * from {$table} where 1=1 and username like '%".$condition."%' and username != 'admin'  order by {$order} limit {$offset},{$num}";
        $this->db->like('username', $condition);
        $this->db->from('login');
        return array(
            'total' => $this->db->count_all_results(),
            'res' => $this->db->query($str_sql)->result_array(),
        ); 
    }
    
    public function newuser(){
        $data['username'] = $this->input->post('username');
        $data['password'] = $this->input->post('password');
        $data['role'] = $this->input->post('role');
        $data['createtime'] = time();
        $data['lastlogintime'] = $data['createtime'];
        $this->db->insert('user', $data);
        return $this->db->insert_id();
    }
    
    public function delete_user(){
        $id = $this->input->get("id");
        $this->db->delete('user', array('id' => $id));
    }

    public function delete_log(){
        $id = $this->input->get("id");
        $this->db->delete('login', array('id' => $id));
    }
    
    public function get_employee($offset,$num,$order='id desc',$condition){
        $str_sql  = "select e.*,u.username from lfsrlzy_employee e left join lfsrlzy_user u on u.id = e.userid where e.name like '%".$condition."%' order by e.{$order} limit {$offset},{$num}";
        $this->db->like('name', $condition);
        $this->db->from('employee');
        return array(
            'total' => $this->db->count_all_results(),
            'res' => $this->db->query($str_sql)->result_array(),
        ); 
    }
    
    public function newemployee(){
        $data['name'] = $this->input->post('name');
        $data['sn'] = $this->input->post('sn');
        $data['department'] = $this->input->post('department');
        $data['workpassholder'] = $this->input->post('workpassholder');
        $data['joindate'] = $this->input->post('joindate');
        $data['position'] = $this->input->post('position');
        $data['birthday'] = $this->input->post('birthday');
        $data['userid'] = $this->input->post('userid');
        $this->db->insert('employee', $data);
        return $this->db->insert_id();
    }
    
    public function delete_employee(){
        $id = $this->input->get("id");
        $this->db->delete('employee', array('id' => $id));
    }
    
    public function getleisureuser(){
        $str_sql = "select u.id,u.username,u.password from lfsrlzy_user u where u.id not in (select uu.id from lfsrlzy_user uu,lfsrlzy_employee e where uu.id = e.userid) and u.role = 1";
        return $this->db->query($str_sql)->result_array();
    }
    
    public function getEmployeeByUserID($userid){
        $query = $this->db->get_where('employee', array('userid' => $userid));
        return $query->row_array();
    }
    
    public function getMarketing(){
        $query = $this->db->get_where('employee', array('department' => 2));
        return $query->result_array();
    }
    
    public function getAllEmployeeUser(){
        $query = $this->db->get_where('user',array('role'=>1));
        return $query->result_array();
    }
    
    public function getAllOffice(){
        $query = $this->db->get_where('employee', array('department' => 1));
        return $query->result_array();
    }
}

?>
